package com.lay.layhttp;

import android.Manifest;
import android.os.Build;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.lay.http.core.DownloadRequest;
import com.lay.http.core.DownloadResponse;
import com.lay.http.core.NetClient;
import com.lay.http.core.GetRequest;
import com.lay.http.core.GetResponse;
import com.lay.http.core.PostRequest;
import com.lay.http.core.PostResponse;
import com.lay.http.ResponseListener;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {
    TextView tv;
    DownloadRequest downloadRequest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        final NetClient client = NetClient.getClient();
        client.init();
        client.addIntercetor(new HeaderInterctor());
        client.addIntercetor(new LogInterctor());
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
        }
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findViewById(R.id.btn_stop).setVisibility(View.VISIBLE);
                tv.setText("");
                String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.jpg";
                final File file = new File(path);
                final long size;
                if(file.exists()){
                    size = file.length();
                } else {
                    size = 0;
                }
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                OutputStream out = null;
                try {
                    out = new FileOutputStream(file, true);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                final OutputStream finalOut = out;
                downloadRequest = client.newDownloadRequest("http://img.netbian.com/file/2019/0824/fe28123a86549be48bc00d639da77c60.jpg")
                    .header("Range", "bytes=" + size + "-")
                    .callback(new ResponseListener<DownloadResponse>() {

                        public void onPreProcess(long totle){
                            Log.d("downloadRequest", "size:" + size + ", totle:" + totle);
                        }

                        @Override
                        public void onResponse(DownloadResponse responseBody) {
                            findViewById(R.id.btn_stop).setVisibility(View.GONE);
                            try {
                                finalOut.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onProcess(byte[] data, long length, long totle){
                            try {
                                finalOut.write(data, 0, data.length);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            tv.setText("current:" + data.length + "\nlength:" + length + "\ntotle:" + totle);
                            Log.d("downloadRequest", "current:" + data.length + ", length:" + length + ", totle:" + totle);
                        }
                    }).bind(MainActivity.this);
                downloadRequest.setLimitDownSpeed(20);
                client.sendAsync(downloadRequest);
            }
        });

        findViewById(R.id.btn_stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(downloadRequest != null){
                    downloadRequest.setWorking(false);
                }
                v.setVisibility(View.GONE);
            }
        });

        findViewById(R.id.btn_get).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("");
                GetRequest requestBody = client.newGetRequest("https://www.wanandroid.com/banner/json")
                    .callback(new ResponseListener<GetResponse>() {
                        @Override
                        public void onResponse(GetResponse responseBody) {
                            System.out.println("response:" + responseBody.getData());
                            tv.setText("response:" + responseBody.getData());
                        }
                    }).bind(MainActivity.this);
                client.sendAsync(requestBody);
            }
        });

        findViewById(R.id.btn_post).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("");
                PostRequest requestBody = client.newPostRequest("https://www.wanandroid.com/user/login")
                    .param("username", "aa112233")
                    .param("password", "123456")
                    .callback(new ResponseListener<PostResponse>() {
                        @Override
                        public void onResponse(PostResponse responseBody) {
                            System.out.println("response:" + responseBody.getData());
                            tv.setText("response:" + responseBody.getData());
                        }
                    }).addIntercetor(new PostHeaderInterctor());
                client.sendAsync(requestBody);
            }
        });
    }
}